//
//  PhotoBrowserTableViewController.m
//  XiangMuB
//
//  Created by lanou3g on 16/8/17.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "PhotoBrowserTableViewController.h"

#import "PhotoTool.h"
#import "PhotoAblumListModel.h"
#import "PhotoBrowserTableViewCell.h"
#import "ThumbnailViewController.h"

@interface PhotoBrowserTableViewController ()

@property (strong, nonatomic) NSMutableArray *arrayDataSource;

@end

@implementation PhotoBrowserTableViewController

- (NSMutableArray *)arrayDataSource{
    
    if (_arrayDataSource == nil) {
        _arrayDataSource = [NSMutableArray array];
    }
    
    return _arrayDataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = [UIColor yellowColor];
    
    self.title = @"照片";
    
    self.edgesForExtendedLayout = UIRectEdgeTop;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self setupNavigation];
    
    [self loadAblums];
    
//    [self pushAllPhotoSoon];
    
    
}

- (void)setupNavigation{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 40, 44);
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn setTitle:@"取消" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(cancelButtonYes)forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.hidesBackButton = YES;
    
}

- (void)loadAblums{
    
    [self.arrayDataSource addObjectsFromArray:[[PhotoTool sharePhotoTool] getPhotoAblumList]];
}

#pragma mark 直接push到所有照片界面
//- (void)pushAllPhotoSoon{
//
//    NSInteger i = 0;
//    
//    for (PhotoAblumListModel *ablum in self.arrayDataSource) {
//        
//        if (ablum.assetCollection.assetCollectionSubtype == 209 || [ablum.title isEqualToString:@"所有照片"]) {
//            
//            i = [self.arrayDataSource indexOfObject:ablum];
//            break;
//            
//        }
//        
//    }
//    
//    [self pushThumbnailVCWithIndex:i animated:NO];
//    
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrayDataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifierCell = @"cell";
    PhotoBrowserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierCell];
    if (cell == nil) {
        
        cell = [[PhotoBrowserTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierCell];
        
    }
    
    PhotoAblumListModel *model = self.arrayDataSource[indexPath.row];
    
    [[PhotoTool sharePhotoTool] requestImageForAsset:model.headImageAsset size:CGSizeMake(65 * 3, 65 * 3) resizeMode:PHImageRequestOptionsResizeModeFast completion:^(UIImage *image, NSDictionary *info) {
       
        cell.headImageView.image = image;
        
    }];
    
    cell.labelTitle.text = model.title;
    cell.labelCount.text = [NSString stringWithFormat:@"(%ld)",(long)model.count];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 66;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self pushThumbnailVCWithIndex:indexPath.row animated:YES];
}

- (void)cancelButtonYes{
    
    if (self.CancelBlock) {
        self.CancelBlock();
    }
    
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)pushThumbnailVCWithIndex:(NSInteger)index animated:(BOOL)animated{
    
    PhotoAblumListModel *model = self.arrayDataSource[index];
    
    ThumbnailViewController *thumbnail = [[ThumbnailViewController alloc] init];
    thumbnail.title = model.title;
    thumbnail.maxSelectCount = self.maxSelectCount;
    thumbnail.isSelectOriginalPhoto = self.isSelectOriginalPhoto;
 
    thumbnail.assetCollection = model.assetCollection;
    thumbnail.arraySelectPhotos = self.arraySelectPhotos;
    thumbnail.browserController = self;
    thumbnail.DoneBlock = self.DoneBlock;
    thumbnail.CancelBlock = self.CancelBlock;
    
    [self.navigationController pushViewController:thumbnail animated:animated];
    
    
}

@end
